package com.yanqu.road.server.manger.wingroom;

import com.yanqu.road.dao.impl.wingroom.WingRoomGuestMessageDaoImpl;
import com.yanqu.road.dao.impl.wingroom.WingRoomMessageBoardDaoImpl;
import com.yanqu.road.entity.enums.eBlacklistType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.wingroom.message.GuestMessage;
import com.yanqu.road.entity.wingroom.message.WingRoomMessageDetailInfo;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.wingroom.WingRoomProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.wingroom.WingRoomMessageModule;
import com.yanqu.road.server.manger.ForbiddenWordMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.player.BlacklistMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.pb.PlayerPb;
import com.yanqu.road.server.pb.WingRoomPb;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

/**
 * @author ：zfr
 * @date ：2022/3/17 17:10
 */
public class WingRoomMessageMgr extends TempMgr {
    private static Map<Long, WingRoomMessageDetailInfo> userMessageMap = new ConcurrentHashMap<>();
    private static Map<Long, List<WingRoomProto.MessageTempMsg.Builder>> messageBuildersMap = new ConcurrentHashMap<>();
    private static Map<Long, Object> lockMap = new ConcurrentHashMap<>();

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        long accessTimeBegin = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(7L);
        userMessageMap = new WingRoomMessageBoardDaoImpl().getWingRoomMessageDetailInfoMap(accessTimeBegin);
        if (userMessageMap.size() > 0) {
            Map<Long, Map<Long, GuestMessage>> guestMessageMap = new WingRoomGuestMessageDaoImpl().getGuestMessageMap(new ArrayList<>(userMessageMap.keySet()), GameConfig.WINGROOM_MESSAGE_STORE_NUM);
            for (WingRoomMessageDetailInfo detailInfo : userMessageMap.values()) {
                if (guestMessageMap.containsKey(detailInfo.getUserId())) {
                    detailInfo.setGuestMessageMap(guestMessageMap.get(detailInfo.getUserId()));
                }
            }
            for (Map<Long, GuestMessage> messageMap : guestMessageMap.values()) {
                List<GuestMessage> messageList = new ArrayList<>(messageMap.values());
                messageList.sort(Comparator.comparing(GuestMessage::isTop).reversed().thenComparing(GuestMessage::getMessageId));
                for (GuestMessage message : messageList) {
                    addMessageBuilder(message.getUserId(), message);
                }
            }
        }
        return true;
    }

    private static Object getMessageLock(long userId) {
        if (!lockMap.containsKey(userId)) {
            synchronized (lockMap) {
                if (!lockMap.containsKey(userId)) {
                    lockMap.put(userId, new Object());
                }
            }
        }
        return lockMap.get(userId);
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static WingRoomMessageDetailInfo getSelfMessageAllData(long userId, boolean isRead) {
        if (userMessageMap.containsKey(userId)) {
            WingRoomMessageDetailInfo messageDetailInfo = userMessageMap.get(userId);
            if (isRead) {
                messageDetailInfo.setOwnerLastRequestTime(System.currentTimeMillis());
                GamePlayer onlinePlayer = GamePlayerMgr.getOnlinePlayer(userId);
                if (onlinePlayer != null) {
                    onlinePlayer.getModule(WingRoomMessageModule.class).syncMsg();
                }
            }
            if (messageBuildersMap.get(userId) == null || messageBuildersMap.get(userId).size() < GameConfig.WINGROOM_MESSAGE_STORE_NUM) {
                long guestMessageCount = messageDetailInfo.getGuestMessageMap().values().stream().filter(message -> !message.isDelete()).count();
                if (messageBuildersMap.get(userId) == null || guestMessageCount > messageBuildersMap.get(userId).size()) {
                    if (messageBuildersMap.get(userId) != null) {
                        messageBuildersMap.put(userId, new ArrayList<>());
                    }
                    List<GuestMessage> tempList = new ArrayList<>(messageDetailInfo.getGuestMessageMap().values());
                    List<GuestMessage> messageList = new ArrayList<>();
                    for(GuestMessage data : tempList){
                        if(!data.isDelete()){
                            messageList.add(data);
                        }
                    }
                    messageList.sort(Comparator.comparing(GuestMessage::isTop).reversed().thenComparing(GuestMessage::getMessageId));
                    for (GuestMessage message : messageList) {
                        addMessageBuilder(message.getUserId(), message);
                    }
                }
            }
            return messageDetailInfo;
        }
        synchronized (getMessageLock(userId)) {
            WingRoomMessageDetailInfo detailInfo = new WingRoomMessageBoardDaoImpl().getWingRoomMessageDetailInfo(userId);
            if (detailInfo != null) {
                userMessageMap.put(detailInfo.getUserId(), detailInfo);
                Map<Long, Map<Long, GuestMessage>> guestMessageMap = new WingRoomGuestMessageDaoImpl().getGuestMessageMap(Collections.singletonList(userId), GameConfig.WINGROOM_MESSAGE_STORE_NUM);
                if (guestMessageMap.containsKey(userId)) {
                    detailInfo.setGuestMessageMap(guestMessageMap.get(userId));
                }
                if (isRead) {
                    detailInfo.setOwnerLastRequestTime(System.currentTimeMillis());
                }
                return detailInfo;
            }
            UserInfo userInfo = UserMgr.getUserInfo(userId);
            BigInteger openValue = BigInteger.ZERO;
            if (null != userInfo) {
                openValue = userInfo.getSystemOpenValue();
            }
            if (!SystemOpenMgr.offlineSystemOpen(openValue, eSystemId.WingRoom.getValue())) {
                return null;
            }
            return initSelfMessageData(userId);
        }
    }

    public static WingRoomMessageDetailInfo getMessageAllData(long ownerUserId) {
        if (userMessageMap.containsKey(ownerUserId)) {
            WingRoomMessageDetailInfo messageDetailInfo = userMessageMap.get(ownerUserId);
            messageDetailInfo.setLastAccessTime(System.currentTimeMillis());
            return messageDetailInfo;
        }
        synchronized (getMessageLock(ownerUserId)) {
            WingRoomMessageDetailInfo detailInfo = new WingRoomMessageBoardDaoImpl().getWingRoomMessageDetailInfo(ownerUserId);
            if (detailInfo != null) {
                userMessageMap.put(detailInfo.getUserId(), detailInfo);
                Map<Long, Map<Long, GuestMessage>> guestMessageMap = new WingRoomGuestMessageDaoImpl().getGuestMessageMap(Collections.singletonList(ownerUserId), GameConfig.WINGROOM_MESSAGE_STORE_NUM);
                if (guestMessageMap.containsKey(ownerUserId)) {
                    detailInfo.setGuestMessageMap(guestMessageMap.get(ownerUserId));
                    List<GuestMessage> messageList = new ArrayList<>(guestMessageMap.get(ownerUserId).values());
                    messageList.sort(Comparator.comparing(GuestMessage::isTop).reversed().thenComparing(GuestMessage::getMessageId));
                    for (GuestMessage message : messageList) {
                        addMessageBuilder(message.getUserId(), message);
                    }
                }

                detailInfo.setLastAccessTime(System.currentTimeMillis());
                return detailInfo;
            }
        }
        return null;
    }

    private static WingRoomMessageDetailInfo initSelfMessageData(long userId) {
        WingRoomMessageDetailInfo messageDetailInfo = new WingRoomMessageDetailInfo();
        messageDetailInfo.setUserId(userId);
        messageDetailInfo.setMessageSwitch(1);
        messageDetailInfo.setLastAccessTime(System.currentTimeMillis());
        messageDetailInfo.setInsertOption();
        userMessageMap.put(userId, messageDetailInfo);
        return messageDetailInfo;
    }

    public static WingRoomProto.ChangeSelfMessageContentRespMsg.Builder changeSelfMessageContent(GamePlayer player, String content) {
        WingRoomProto.ChangeSelfMessageContentRespMsg.Builder respMsg = WingRoomProto.ChangeSelfMessageContentRespMsg.newBuilder();
        synchronized (getMessageLock(player.getUserId())) {
            WingRoomMessageDetailInfo messageAllData = getSelfMessageAllData(player.getUserId(), false);
            if (messageAllData == null) {
                respMsg.setRet(GameErrorCode.E_WING_ROOM_LOCK);
                return respMsg;
            }
            content = StringUtils.clearHeadAndTailEmpty(content);
            int stringLen = StringUtils.getStringLen(content);
            if (stringLen > GameConfig.WINGROOM_OWNER_MESSAGE_WORD_COUNT) {
                respMsg.setRet(GameErrorCode.E_WING_ROOM_MESSAGE_CONTENT_LENGTH_LIMIT);
                return respMsg;
            }
            if (ForbiddenWordMgr.isForbidden(content)) {
                respMsg.setRet(GameErrorCode.E_FORBIDDEN_WORD);
                return respMsg;
            }

            messageAllData.setSelfMessageContent(content);
            respMsg.setRet(0);
            respMsg.setContent(content);

        }
        return respMsg;
    }

    public static WingRoomProto.ChangeMessageSwitchRespMsg.Builder changeSelfMessageSwitch(GamePlayer player, int switchType) {
        WingRoomProto.ChangeMessageSwitchRespMsg.Builder respMsg = WingRoomProto.ChangeMessageSwitchRespMsg.newBuilder();
        synchronized (getMessageLock(player.getUserId())) {
            WingRoomMessageDetailInfo messageAllData = getSelfMessageAllData(player.getUserId(), false);
            if (messageAllData == null) {
                respMsg.setRet(GameErrorCode.E_WING_ROOM_LOCK);
                respMsg.setSwitchType(0);
                return respMsg;
            }
            messageAllData.setMessageSwitch(switchType);
            respMsg.setRet(0);
            respMsg.setSwitchType(switchType);
        }
        return respMsg;
    }

    public static WingRoomProto.NewMessageRespMsg.Builder newMessage(long ownerUserId, String content, int noteStyle,
                                                                     long guestServerId, long guestUserId, List<Long> relativesIds) {
        WingRoomProto.NewMessageRespMsg.Builder respMsg = WingRoomProto.NewMessageRespMsg.newBuilder();
        if(BlacklistMgr.isInBlacklist(ownerUserId, eBlacklistType.WingRoom.getValue(), guestUserId, guestServerId)){
            respMsg.setRet(GameErrorCode.E_IN_PLAYER_BLACKLIST);
            return respMsg;
        }
        synchronized (getMessageLock(ownerUserId)) {
            WingRoomMessageDetailInfo messageAllData = getMessageAllData(ownerUserId);
            if (messageAllData == null) {
                respMsg.setRet(GameErrorCode.E_WING_ROOM_MESSAGE_NO_FOUND);
                return respMsg;
            }
            if (messageAllData.getMessageSwitch() == 0) {
                if (!relativesIds.contains(ownerUserId)) {
                    respMsg.setRet(GameErrorCode.E_WING_ROOM_ONLY_RELATIVES_MESSAGE);
                    return respMsg;
                }
            }
            GuestMessage message = addMessage(ownerUserId, content, noteStyle, guestServerId, guestUserId, messageAllData);
            long removeMessageId = addMessageBuilder(messageAllData.getUserId(), message);
            respMsg.setRet(0);
            respMsg.setGuestMessage(WingRoomPb.buildMessageTemp(message));
            if (removeMessageId > 0) {
                respMsg.setRemoveMessageId(removeMessageId);
            }
            GamePlayer onlinePlayer = GamePlayerMgr.getOnlinePlayer(ownerUserId);
            if (onlinePlayer != null) {
                onlinePlayer.getModule(WingRoomMessageModule.class).syncMsg();
            }
            return respMsg;
        }
    }

    public static WingRoomProto.GetMessageDetailRespMsg.Builder getMessageDetailInfo(long ownerUserId, long messageId, long guestServerId, long guestUserId) {
        WingRoomProto.GetMessageDetailRespMsg.Builder respMsg = WingRoomProto.GetMessageDetailRespMsg.newBuilder();
        respMsg.setRet(0);
        WingRoomMessageDetailInfo messageAllData;
        if (ownerUserId == guestUserId) {
            messageAllData = getSelfMessageAllData(ownerUserId, false);
        } else {
            messageAllData = getMessageAllData(ownerUserId);
        }
        if (messageAllData == null) {
            respMsg.setRet(GameErrorCode.E_WING_ROOM_MESSAGE_NO_FOUND);
            return respMsg;
        }
        GuestMessage message = messageAllData.getGuestMessageMap().get(messageId);
        if (message == null || message.isDelete()) {
            respMsg.setRet(GameErrorCode.E_WING_ROOM_GUEST_MESSAGE_NO_FOUND);
            return respMsg;
        }
        if (ownerUserId == guestUserId) {
            changeReadStatus(message);
        }
        respMsg.setPlayerId(message.getGuestUserId());
        respMsg.setContent(message.getContent());
        respMsg.setNoteStyle(message.getNoteStyle());
        respMsg.setMessageTime(message.getMessageTime());
        respMsg.setMessageId(messageId);
        return buildMessageDetailUserBaseInfo(respMsg, message, guestServerId, guestUserId);
    }

    private static WingRoomProto.GetMessageDetailRespMsg.Builder buildMessageDetailUserBaseInfo(WingRoomProto.GetMessageDetailRespMsg.Builder respMsg, GuestMessage message, long guestServerId, long guestUserId) {
        //本服玩家留的言
        if (message.getGuestServerId() == GameServer.getInstance().getServerId()) {
            UserInfo userInfo = UserMgr.getUserInfo(message.getGuestUserId());
            if (userInfo != null) {
                respMsg.setMessagePlayerInfo(PlayerPb.parsePlayerBaseTempMsg(userInfo));
            }
            return respMsg;
        }
        WingRoomProto.GetMessageDetailUserInfoReqMsg.Builder reqCrossMsg = WingRoomProto.GetMessageDetailUserInfoReqMsg.newBuilder();
        reqCrossMsg.setGuestServerId(guestServerId);
        reqCrossMsg.setGuestUserId(guestUserId);
        reqCrossMsg.setRespMsg(respMsg);
        YanQuMessage msg = YanQuMessageUtils.buildMessage(CrossProtocol.C_WING_ROOM_GET_MESSAGE_DETAIL_USER_INFO, reqCrossMsg);
        GamePlayerMgr.sendPacket(0, msg);
        return null;
    }

    private static void changeReadStatus(GuestMessage message) {
        message.setRead(true);
        List<WingRoomProto.MessageTempMsg.Builder> builders = messageBuildersMap.get(message.getUserId());
        for (WingRoomProto.MessageTempMsg.Builder builder : builders) {
            if (builder.getMessageId() == message.getMessageId()) {
                builder.setIsRead(true);
                return;
            }
        }
    }

    //置顶留言
    public static void topMessage(GamePlayer player, long messageId, boolean isTop) {
        WingRoomProto.TopMessageRespMsg.Builder respMsg = WingRoomProto.TopMessageRespMsg.newBuilder();
        WingRoomMessageDetailInfo messageAllData = getSelfMessageAllData(player.getUserId(), false);
        if (messageAllData == null) {
            respMsg.setRet(GameErrorCode.E_WING_ROOM_MESSAGE_NO_FOUND);
            return;
        }
        GuestMessage message = messageAllData.getGuestMessageMap().get(messageId);
        if (message == null || message.isDelete()) {
            respMsg.setRet(GameErrorCode.E_WING_ROOM_GUEST_MESSAGE_NO_FOUND);
            return;
        }
        long nowTop = messageAllData.getGuestMessageMap().values().stream().filter(msg -> !msg.isDelete() && msg.isTop()).count();
        if (!message.isTop() && isTop && nowTop >= GameConfig.WINGROOM_MESSAGE_TOP_NUM) {
            respMsg.setRet(GameErrorCode.E_WING_ROOM_TOP_MESSAGE_LIMIT);
            return;
        }
        List<WingRoomProto.MessageTempMsg.Builder> builders = messageBuildersMap.get(player.getUserId());
        for (WingRoomProto.MessageTempMsg.Builder builder : builders) {
            if (builder.getMessageId() == messageId) {
                builder.setIsTop(isTop);
            }
        }
        message.setTop(isTop);
        respMsg.setRet(0);
        respMsg.setGuestMessage(WingRoomPb.buildMessageTemp(message));
        player.sendPacket(ClientProtocol.U_WING_ROOM_TOP_MESSAGE, respMsg);
    }


    public static WingRoomProto.GiveMessageLikeRespMsg.Builder giveMessageLike(long ownerUserId, long guestUserId) {
        WingRoomProto.GiveMessageLikeRespMsg.Builder respMsg = WingRoomProto.GiveMessageLikeRespMsg.newBuilder();
        synchronized (getMessageLock(ownerUserId)) {
            WingRoomMessageDetailInfo messageAllData;
            if (ownerUserId == guestUserId) {
                messageAllData = getSelfMessageAllData(ownerUserId, false);
            } else {
                messageAllData = getMessageAllData(ownerUserId);
            }
            if (messageAllData == null) {
                respMsg.setRet(GameErrorCode.E_WING_ROOM_MESSAGE_NO_FOUND);
                return respMsg;
            }
            messageAllData.setSelfMessageGiveLikeTimes(messageAllData.getSelfMessageGiveLikeTimes() + 1);
            respMsg.setRet(0);
            respMsg.setGiveLikes(messageAllData.getSelfMessageGiveLikeTimes());
        }
        return respMsg;
    }

    public static void deleteMessage(GamePlayer player, long messageId) {
        WingRoomProto.DeleterMessageRespMsg.Builder respMsg = WingRoomProto.DeleterMessageRespMsg.newBuilder();
        WingRoomMessageDetailInfo messageAllData = getSelfMessageAllData(player.getUserId(), false);
        if (messageAllData == null) {
            respMsg.setRet(GameErrorCode.E_WING_ROOM_MESSAGE_NO_FOUND);
            player.sendPacket(ClientProtocol.U_WING_ROOM_DELETE_MESSAGE, respMsg);
            return;
        }
        GuestMessage message = messageAllData.getGuestMessageMap().get(messageId);
        if (message == null) {
            respMsg.setRet(GameErrorCode.E_WING_ROOM_GUEST_MESSAGE_NO_FOUND);
            player.sendPacket(ClientProtocol.U_WING_ROOM_DELETE_MESSAGE, respMsg);
            return;
        }
        message.setDelete(true);
        List<WingRoomProto.MessageTempMsg.Builder> builders = messageBuildersMap.get(player.getUserId());
        if (builders != null) {
            for (WingRoomProto.MessageTempMsg.Builder builder : builders) {
                if (builder.getMessageId() == messageId) {
                    builders.remove(builder);
                    break;
                }
            }
        }
        respMsg.setRet(0);
        player.sendPacket(ClientProtocol.U_WING_ROOM_DELETE_MESSAGE, respMsg);
    }

    public static boolean checkRedDot(long userId) {
        WingRoomMessageDetailInfo selfMessageAllData = getSelfMessageAllData(userId, false);
        List<WingRoomProto.MessageTempMsg.Builder> builders = messageBuildersMap.get(userId);
        if (selfMessageAllData != null && builders != null && builders.size() > 0) {
            WingRoomProto.MessageTempMsg.Builder builder = builders.get(builders.size() - 1);
            if (builder != null) {
                GuestMessage message = selfMessageAllData.getGuestMessageMap().get(builder.getMessageId());
                if (message != null) {
                    return message.getMessageTime() > selfMessageAllData.getOwnerLastRequestTime();
                }
            }
        }
        return false;
    }

    public static GuestMessage addMessage(long ownerUserId, String content, int noteStyle, long serverId, long userId, WingRoomMessageDetailInfo messageDetailInfo) {
        GuestMessage message = new GuestMessage();
        message.setUserId(ownerUserId);
        message.setMessageId(messageDetailInfo.getNextMessageId());
        message.setContent(content);
        message.setNoteStyle(noteStyle);
        message.setGuestServerId(serverId);
        message.setGuestUserId(userId);
        message.setMessageTime(System.currentTimeMillis());
        message.setInsertOption();
        messageDetailInfo.putMessage(message);
        return message;
    }

    private static long addMessageBuilder(long userId, GuestMessage message) {
        long removeMessageId = 0;
        if (!messageBuildersMap.containsKey(userId)) {
            messageBuildersMap.put(userId, new ArrayList<>());
        }
        List<WingRoomProto.MessageTempMsg.Builder> builders = messageBuildersMap.get(userId);
        WingRoomProto.MessageTempMsg.Builder messageTemp = WingRoomPb.buildMessageTemp(message);
        if (builders.size() > 0 && builders.size() >= GameConfig.WINGROOM_MESSAGE_STORE_NUM) {
            int beginIndex = 0;
            while (builders.size() > beginIndex) {
                WingRoomProto.MessageTempMsg.Builder builder = builders.get(beginIndex);
                if (!builder.getIsTop()) {
                    removeMessageId = builders.get(beginIndex).getMessageId();
                    builders.remove(beginIndex);
                    break;
                }
                beginIndex++;
            }
        }
        builders.add(messageTemp);
        return removeMessageId;
    }

    @Override
    public boolean save() {
        for (WingRoomMessageDetailInfo detailInfo : userMessageMap.values()) {
            if (detailInfo.isInsertOption()) {
                new WingRoomMessageBoardDaoImpl().add(detailInfo);
            } else if (detailInfo.isUpdateOption()) {
                new WingRoomMessageBoardDaoImpl().update(detailInfo);
            }
            for (GuestMessage message : detailInfo.getGuestMessageMap().values()) {
                if (message.isInsertOption()) {
                    new WingRoomGuestMessageDaoImpl().add(message);
                } else if (message.isUpdateOption()) {
                    new WingRoomGuestMessageDaoImpl().update(message);
                }
            }
        }
        return true;
    }

    public static WingRoomProto.GetMessageAllDataRespMsg.Builder getMessageAllDataResp(long ownerUserId, long guestUserId, boolean likeToday) {
        WingRoomProto.GetMessageAllDataRespMsg.Builder respMsg = WingRoomProto.GetMessageAllDataRespMsg.newBuilder();
        respMsg.setRet(0);
        WingRoomMessageDetailInfo messageAllData;
        if (ownerUserId == guestUserId) {
            messageAllData = WingRoomMessageMgr.getSelfMessageAllData(ownerUserId, true);
        } else {
            messageAllData = WingRoomMessageMgr.getMessageAllData(ownerUserId);
        }
        if (messageAllData == null) {
            respMsg.setRet(GameErrorCode.E_WING_ROOM_GET_MESSAGE_FAIL);
            return respMsg;
        }
        List<WingRoomProto.MessageTempMsg.Builder> builders = messageBuildersMap.get(ownerUserId);
        WingRoomProto.MessageAllDataMsg.Builder builder = WingRoomPb.buildWingRoomMessageAllData(messageAllData, likeToday, builders);
        respMsg.setMessageData(builder);
        return respMsg;
    }

    /**
    * 描述：处理黑名单的留言
    * 作者：zrq
    * 时间：2023/1/3
    * 参数：

    * 返回值：
    **/
    public static void blacklistRemoveMessage(long userId, long blacklistUserId, long blacklistServerId) {
        WingRoomMessageDetailInfo messageAllData = getSelfMessageAllData(userId, false);
        if (null != messageAllData) {
            List<Long> removeList = new ArrayList<>();
            for(GuestMessage message : messageAllData.getGuestMessageMap().values()){
                if(message.getGuestUserId() == blacklistUserId && message.getGuestServerId() == blacklistServerId){
                    message.setDelete(true);
                    removeList.add(message.getMessageId());
                }
            }

            List<WingRoomProto.MessageTempMsg.Builder> builderList = messageBuildersMap.get(userId);
            if (null != builderList) {
                builderList.removeIf(builder -> removeList.contains(builder.getMessageId()));
            }
        }
    }
}
